package com.cogent.business.action.base;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.cogent.business.action.AbstractAction;
import com.cogent.business.cache.CacheRefreshUtil;
import com.cogent.business.helper.DynamicFormHelper;
import com.cogent.business.helper.UserDomainHelper;
import com.cogent.business.helper.ValidatorHelper;
import com.cogent.business.helper.WorkFolwHelper;
import com.cogent.common.io.FileUtil;
import com.cogent.common.util.BaseUtil;
import com.cogent.common.util.ClassReflectUtil;
import com.cogent.common.util.ConstantUtil;
import com.cogent.common.util.Globals;
import com.cogent.common.util.HtmlUtil;
import com.cogent.common.util.MapUtil;
import com.cogent.common.util.StringUtil;
import com.cogent.dao.BussinessQueryHelper;
import com.cogent.dao.domain.Pagination;
import com.cogent.dao.domain.QueryParamDomain;
import com.cogent.dao.domain.TableInfoDomain;
import com.cogent.dynamicform.bus.ICustomValidator;
import com.cogent.dynamicform.domain.FormDomain;
import com.cogent.dynamicform.domain.FormQueryDynTableDomain;
import com.cogent.dynamicform.domain.FormViewDynTableDomain;
import com.cogent.dynamicform.facade.IDynamicFormFacade;
import com.cogent.dynamicform.facade.impl.DynamicFormFacadeImpl;
import com.cogent.dynamicform.util.DynamicFormBusUtil;

public class DynamicFormAction extends AbstractAction {
	IDynamicFormFacade dynamicFormFacade = new DynamicFormFacadeImpl();
	private final static String UPLOAD_DIR = "upload";
	private java.util.List<File> upload; 
	private java.util.List<String> uploadFileName; 
	private java.util.List<String> uploadContentType; 

	public String list() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if(!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.VIEW))
			return "noright";
		if(formDomain.isQueryprefix())
			formMap.put("status", "0");
		List queryList = dynamicFormFacade.getQueryList(formDomain, formMap);
		String sql = formDomain.getQuerysql();
		if((StringUtil.isEmpty(sql)) && (!formDomain.getOperatorbut().equals("NO")))
			queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = null;
		if(StringUtil.isEmpty(sql))
			pagination = bussinessFacade.queryPaged(formDomain.getMaptable(), queryList, formDomain.getOrderby(), MapUtil.getIntValue(formMap, "page", 1)
					, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		else {
			String groupby = "";
			int index = sql.indexOf(" group by ");
			if(index > 0){
				sql = sql.substring(0, index);
				groupby = formDomain.getQuerysql().substring(index);
			}
			pagination = bussinessFacade.queryWithPrefixPaged(sql, queryList, formDomain.getOrderby() + groupby, MapUtil.getIntValue(formMap, "page", 1)
					, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		}
		
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "list";
	}
	
	public String listrec() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if(!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode() + "_REC", Globals.VIEW))
			return "noright";
		if(formDomain.isQueryprefix())
			formMap.put("status", "1");
		List queryList = dynamicFormFacade.getQueryList(formDomain, formMap);
		if(!formDomain.isQueryprefix())
			queryList.add(new QueryParamDomain("status", "1"));
		Pagination pagination = null;
		if(StringUtil.isEmpty(formDomain.getQuerysql()))
			pagination = bussinessFacade.queryPaged(formDomain.getMaptable(), queryList, formDomain.getOrderby(), MapUtil.getIntValue(formMap, "page", 1)
					, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		else {
			String sql = formDomain.getQuerysql();
			String groupby = "";
			int index = sql.indexOf(" group by ");
			if(index > 0){
				sql = sql.substring(0, index);
				groupby = formDomain.getQuerysql().substring(index);
			}
			pagination = bussinessFacade.queryWithPrefixPaged(sql, queryList, groupby, MapUtil.getIntValue(formMap, "page", 1)
					, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		}
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "listrec";
	}
	
	public String add() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if(!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.SAVE))
			return "noright";
		dynamicFormFacade.getAddValue(formDomain, formMap);
		setResponseMsg("formmap", formMap);
		
		return "add";
	}
	
	public String save() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if((!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.SAVE)) && (!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.UPDATE)))
			return "noright";
		if((MapUtil.isEmpty(formMap, "id") && (!StringUtil.isEmpty(formDomain.getFormmapfield())))) {
			String[] temps = formDomain.getFormmapfield().split(";");
			for(int i=0; i<temps.length; i++) {
				String[] temps2 = temps[i].split("~");
				if(MapUtil.isEmpty(formMap, temps2[0]))
					formMap.put(temps2[0], MapUtil.getStringValue(formMap, temps2[1]));
			}
		}
		if((upload != null) && (upload.size() > 0)) {
			String photosrc = "";
			String realfileName = "";
			for(int i=0; i<upload.size(); i++) {
				File photo = (File)upload.get(i);
				String postfile = FileUtil.getFilePostfix(uploadFileName.get(i));
				String curuuid = BaseUtil.genUUID();
				String fileName =  UPLOAD_DIR + File.separator + curuuid + "." + postfile;
				FileUtil.copyFile(photo, Globals.getProperty("uploaddir") + fileName, true);
				photosrc += Globals.getProperty("uploadurl") + UPLOAD_DIR + "/" + curuuid + "." + postfile + ";";
				realfileName += uploadFileName.get(i) + ";";
				FileUtil.deleteFile(photo);
			}
			formMap.put(DynamicFormBusUtil.DYNAMICFORM_FILPATH, photosrc.substring(0, photosrc.length()-1));
			formMap.put(DynamicFormBusUtil.DYNAMICFORM_REALFILENAME, realfileName.substring(0, realfileName.length()-1));
		}
		if((formDomain.getDynTableList() != null) && (formDomain.getDynTableList().size() > 0))
			DynamicFormBusUtil.dealDynTableMap(formDomain.getDynTableList(), formMap);
		if(formDomain.getQueryDynTableDomain() != null)	
			DynamicFormBusUtil.dealQueryDynTableMap(formDomain.getQueryDynTableDomain().getDynTableColumnList(), formMap);
		dynamicFormFacade.dealFormValue(formDomain, formMap);
		String errmsg = ValidatorHelper.validator(WorkFolwHelper.getWorkFlowValidatorList(formMap, formDomain), formMap);
		if(StringUtil.isEmpty(errmsg)) {
			if(!StringUtil.isEmpty(formDomain.getCheckexist())) {
				String[] temp = formDomain.getCheckexist().split("\\|");
				List curQueryList = new ArrayList();				
				if(temp.length > 1) {
					String[] temp1 = temp[1].split(";");
					for(int i=0; i<temp1.length; i++) {
						String[] temp2 = temp1[i].split("~");
						curQueryList.add(new QueryParamDomain(temp2[0], temp2[1], MapUtil.getStringValue(formMap, temp2[1]), StringUtil.toInt(temp2[2], 1)));
					}
				}
				HashMap curdataMap = bussinessFacade.getOneRow(temp[0], curQueryList);
				if(MapUtil.getIntValue(curdataMap, "nums") > 0)
					errmsg = temp[2];
			}
		}
		if(StringUtil.isEmpty(errmsg) && (!StringUtil.isEmpty(formDomain.getCustomvalidator()))) {
			ICustomValidator customvalidator = (ICustomValidator)ClassReflectUtil.getNewInstance(formDomain.getCustomvalidator());
			errmsg = customvalidator.validator(bussinessFacade, formDomain, formMap);
		}			
		if(!StringUtil.isEmpty(errmsg)) {
			setResponseMsg("errmsg", errmsg);
			setResponseMsg("formmap", formMap);
			return "mod";
		} else {
			String id = dynamicFormFacade.dealBussiness(bussinessFacade, formDomain, formMap);
			String curret = "";
			if(!MapUtil.isEmpty(formMap, ConstantUtil.BUSSINESS_ERR_NOTE_KEY)) {
				setResponseMsg("errmsg", MapUtil.getStringValue(formMap, ConstantUtil.BUSSINESS_ERR_NOTE_KEY));
				setResponseMsg("formmap", formMap);
				curret = "mod";
			}
			if(MapUtil.getIntValue(formMap, "is_open_close") == 1)
				formDomain.setOpenertype("CLOSE");
			formMap.put("id", id);
			if((!StringUtil.isEmpty(formDomain.getOpenertype())) && (!formDomain.getReturntype().equals("LIST"))){
				if(formDomain.getOpenertype().equals("CLOSE"))
					formMap.put("is_closed", "1");
				setResponseMsg("datamap", formMap);
				setResponseMsg("formmap", formMap);
				curret = "openermod";
			}else if(((!StringUtil.isEmpty(formDomain.getLinktaps())) || (!StringUtil.isEmpty(formDomain.getParenttaps()))) && (!formDomain.getReturntype().equals("LIST"))){
				if((formDomain.getDynTabletype()!= null) && (formDomain.getDynTabletype().equals("VIEW"))) {
					List queryList = new ArrayList();
					queryList.add(new QueryParamDomain(formDomain.getDynTableParentid(), MapUtil.getStringValue(formMap, "id")));
					queryList.add(new QueryParamDomain("status", "0"));
					List curList = bussinessFacade.queryWithPostfix(formDomain.getDynTableName(), queryList, formDomain.getDynTableOrderBy());
					formMap.put(DynamicFormHelper.DYNTABLE_FORM_KEY, curList);
				}
				if(formDomain.getQueryDynTableDomain() != null) {
					FormQueryDynTableDomain queryDynTableDomain = formDomain.getQueryDynTableDomain();
					List queryList = new ArrayList();
					queryList.add(new QueryParamDomain(queryDynTableDomain.getParentid(), MapUtil.getStringValue(formMap, "id")));
					queryList.add(new QueryParamDomain("status", "0"));
					List curList = bussinessFacade.queryWithPostfix(queryDynTableDomain.getMaptable(), queryList, queryDynTableDomain.getOrderby());
					formMap.put(DynamicFormHelper.QUERY_DYNTABLE_FORM_KEY, curList);
				}
				setResponseMsg("datamap", formMap);
				setResponseMsg("formmap", formMap);
				curret = "mod";
			} 
			if(!StringUtil.isEmpty(curret)) {
				if(formDomain.isIscache())
					BussinessQueryHelper.removeByKey(formDomain.getMaptable().toUpperCase());
            					if(!StringUtil.isEmpty(formDomain.getCacherefreshcode()))
					CacheRefreshUtil.refreshByCode(formDomain.getCacherefreshcode(), formMap);
				return curret;
			} else {
				if(formDomain.isIscache())
					BussinessQueryHelper.removeByKey(formDomain.getMaptable().toUpperCase());
            		if(!StringUtil.isEmpty(formDomain.getCacherefreshcode()))
					CacheRefreshUtil.refreshByCode(formDomain.getCacherefreshcode(), formMap);
				String url = "DynamicForm_list.action?formid=" + formid;
				String referer = MapUtil.getStringValue(formMap, "referer");
				if(StringUtil.isEmpty(referer)) {						
					if(!StringUtil.isEmpty(formDomain.getUrlpost()))
						url += "&" + formDomain.getUrlpost();
					if(!StringUtil.isEmpty(formDomain.getBacksuffix()))
						url += "&" + formDomain.getBacksuffix();
					setResponseUrl(formMap, url);
				} else
					setResponseUrl(HtmlUtil.getUnEscapeReferer(referer));
			}
		}
		
		return null;
	}
	
	public String resume() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if(!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode() + "_REC", Globals.RESUME))
			return "noright";
		HashMap dataMap = new HashMap();
		dataMap.put("id", MapUtil.getStringValue(formMap, "id"));
		dataMap.put("modifier", MapUtil.getStringValue(formMap, "modifier"));
		dataMap.put("modifydate", MapUtil.getStringValue(formMap, "modifydate"));
		dataMap.put("status", "0");
		dynamicFormFacade.dealBussiness(bussinessFacade, formDomain, dataMap);
		String url = "DynamicForm_listrec.action?formid=" + formid;
		if(!StringUtil.isEmpty(formDomain.getBacksuffix()))
			url += "&" + formDomain.getBacksuffix();
		if(formDomain.isIscache())
			BussinessQueryHelper.removeByKey(formDomain.getMaptable().toUpperCase());
    		setResponseUrl(formMap, url);
		
		return null;
	}
	
	public String mod() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		String parentid = formDomain.getParentid();		
		if(!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.UPDATE))
			return "noright";
		try{
			bussinessFacade.createConn();
			List queryList = new ArrayList();
			HashMap dataMap = new HashMap();
			if(!StringUtil.isEmpty(formDomain.getMaptable())) {
				if(MapUtil.isEmpty(formMap, "id"))
					queryList.add(new QueryParamDomain(formDomain.getLinkid(), MapUtil.getStringValue(formMap, formDomain.getLinkid())));
				else
					queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id")));
				dataMap = bussinessFacade.getByID(formDomain.getMaptable(), queryList);
			}
			if((formDomain.getDynTableList() != null) && (formDomain.getDynTableList().size() > 0)) {
				queryList = new ArrayList();
				String curid = MapUtil.getStringValue(dataMap, "id");
				if(StringUtil.isEmpty(curid)) {
					curid = MapUtil.getStringValue(formMap, formDomain.getDynTableParentid());
					dataMap.put(formDomain.getDynTableParentid(), curid);
				}
				if(StringUtil.isEmpty(curid)) {
					curid = MapUtil.getStringValue(formMap, "id");
					dataMap.put(formDomain.getDynTableParentid(), curid);
				}
				queryList.add(new QueryParamDomain(formDomain.getDynTableParentid(), curid));
				queryList.add(new QueryParamDomain("status", "0"));
				List curList = bussinessFacade.queryWithPostfix(formDomain.getDynTableName(), queryList, formDomain.getDynTableOrderBy());
				dataMap.put(DynamicFormHelper.DYNTABLE_FORM_KEY, curList);
			}
			if(formDomain.getQueryDynTableDomain() != null) {
				FormQueryDynTableDomain queryDynTableDomain = formDomain.getQueryDynTableDomain();
				queryList = new ArrayList();
				queryList.add(new QueryParamDomain(queryDynTableDomain.getParentid(), MapUtil.getStringValue(dataMap, "id")));
				queryList.add(new QueryParamDomain("status", "0"));
				List curList = bussinessFacade.queryWithPostfix(queryDynTableDomain.getMaptable(), queryList, queryDynTableDomain.getOrderby());
				dataMap.put(DynamicFormHelper.QUERY_DYNTABLE_FORM_KEY, curList);
			}
			if(formDomain.getViewDynTableDomain() != null) {
				FormViewDynTableDomain viewDynTableDomain = formDomain.getViewDynTableDomain();
				queryList = new ArrayList();
				String curid = MapUtil.getStringValue(dataMap, "id");
				if(!StringUtil.isEmpty(viewDynTableDomain.getMapparentid()))
					curid = MapUtil.getStringValue(dataMap, viewDynTableDomain.getMapparentid());				
				if(StringUtil.isEmpty(curid)) {
					curid = MapUtil.getStringValue(formMap, viewDynTableDomain.getParentid());
					dataMap.put(viewDynTableDomain.getParentid(), curid);
				}
				if(StringUtil.isEmpty(curid)) {
					curid = MapUtil.getStringValue(formMap, "id");
					dataMap.put(viewDynTableDomain.getParentid(), curid);
				}
				queryList.add(new QueryParamDomain(viewDynTableDomain.getParentid(), curid));
				queryList.add(new QueryParamDomain("status", "0"));
				List curList = bussinessFacade.queryWithPostfix(viewDynTableDomain.getMaptable(), queryList, viewDynTableDomain.getOrderby());
				dataMap.put(DynamicFormHelper.DYNTABLE_FORM_VIEW_KEY, curList);
			}
			if(dataMap.size() > 0)
				dataMap.put("USER_DISTRICTCODE", MapUtil.getStringValue(formMap, "USER_DISTRICTCODE"));
			if(!StringUtil.isEmpty(parentid)) {
				if(MapUtil.isEmpty(dataMap, parentid))
					dataMap.put(parentid, MapUtil.getStringValue(formMap, "id"));
			}
			setResponseMsg("formmap", formMap);
			setResponseMsg("datamap", dataMap);
		}finally{
			bussinessFacade.closeConn();
		}
		
		return "mod";
	}
	
	public String view() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if((!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.VIEW)) && (!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode() + "_REC", Globals.VIEW)))
			return "noright";
		try{
			bussinessFacade.createConn();
			List queryList = new ArrayList();
			HashMap dataMap = new HashMap();
			if(!StringUtil.isEmpty(formDomain.getMaptable())) {
				if(MapUtil.isEmpty(formMap, "id"))
					queryList.add(new QueryParamDomain(formDomain.getLinkid(), MapUtil.getStringValue(formMap, formDomain.getLinkid())));
				else
					queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id")));
				dataMap = bussinessFacade.getByID(formDomain.getMaptable(), queryList);
			}
			if((formDomain.getDynTableList() != null) && (formDomain.getDynTableList().size() > 0)) {
				queryList = new ArrayList();
				String curid = MapUtil.getStringValue(dataMap, "id");
				if(StringUtil.isEmpty(curid)) {
					curid = MapUtil.getStringValue(formMap, formDomain.getDynTableParentid());
					dataMap.put(formDomain.getDynTableParentid(), curid);
				}
				if(StringUtil.isEmpty(curid)) {
					curid = MapUtil.getStringValue(formMap, "id");
					dataMap.put(formDomain.getDynTableParentid(), curid);
				}
				queryList.add(new QueryParamDomain(formDomain.getDynTableParentid(), curid));
				queryList.add(new QueryParamDomain("status", "0"));
				List curList = bussinessFacade.queryWithPostfix(formDomain.getDynTableName(), queryList, formDomain.getDynTableOrderBy());
				dataMap.put(DynamicFormHelper.DYNTABLE_FORM_KEY, curList);
			}
			if(formDomain.getQueryDynTableDomain() != null) {
				FormQueryDynTableDomain queryDynTableDomain = formDomain.getQueryDynTableDomain();
				queryList = new ArrayList();
				queryList.add(new QueryParamDomain(queryDynTableDomain.getParentid(), MapUtil.getStringValue(dataMap, "id")));
				queryList.add(new QueryParamDomain("status", "0"));
				List curList = bussinessFacade.queryWithPostfix(queryDynTableDomain.getMaptable(), queryList, queryDynTableDomain.getOrderby());
				dataMap.put(DynamicFormHelper.QUERY_DYNTABLE_FORM_KEY, curList);
			}
			if(formDomain.getViewDynTableDomain() != null) {
				FormViewDynTableDomain viewDynTableDomain = formDomain.getViewDynTableDomain();
				queryList = new ArrayList();
				String curid = MapUtil.getStringValue(dataMap, "id");
				if(!StringUtil.isEmpty(viewDynTableDomain.getMapparentid()))
					curid = MapUtil.getStringValue(dataMap, viewDynTableDomain.getMapparentid());				
				if(StringUtil.isEmpty(curid)) {
					curid = MapUtil.getStringValue(formMap, viewDynTableDomain.getParentid());
					dataMap.put(viewDynTableDomain.getParentid(), curid);
				}
				if(StringUtil.isEmpty(curid)) {
					curid = MapUtil.getStringValue(formMap, "id");
					dataMap.put(viewDynTableDomain.getParentid(), curid);
				}
				queryList.add(new QueryParamDomain(viewDynTableDomain.getParentid(), curid));
				queryList.add(new QueryParamDomain("status", "0"));
				List curList = bussinessFacade.queryWithPostfix(viewDynTableDomain.getMaptable(), queryList, viewDynTableDomain.getOrderby());
				dataMap.put(DynamicFormHelper.DYNTABLE_FORM_VIEW_KEY, curList);
			}
			setResponseMsg("formmap", formMap);
			setResponseMsg("datamap", dataMap);
		}finally{
			bussinessFacade.closeConn();
		}
		
		return "view";
	}
	
	public String recycle() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if(!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.RECYCLE))
			return "noright";
		HashMap dataMap = new HashMap();
		dataMap.put("id", MapUtil.getStringValue(formMap, "id"));
		dataMap.put("modifier", MapUtil.getStringValue(formMap, "modifier"));
		dataMap.put("modifydate", MapUtil.getStringValue(formMap, "modifydate"));
		dataMap.put("status", "1");
		dynamicFormFacade.dealBussiness(bussinessFacade, formDomain, dataMap);
		String url = "DynamicForm_list.action?formid=" + formid;
		if(!StringUtil.isEmpty(formDomain.getBacksuffix()))
			url += "&" + formDomain.getBacksuffix();
		if(formDomain.isIscache())
			BussinessQueryHelper.removeByKey(formDomain.getMaptable().toUpperCase());
    	setResponseUrl(formMap, url);
		
		return null;
	}
	
	public String openerlist() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if(!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.UPDATE))
			return "noright";
		List queryList = new ArrayList();
		String[] linkids = formDomain.getLinkid().split("~");
		for(int i=0; i<linkids.length; i++)
			queryList.add(new QueryParamDomain(linkids[i], MapUtil.getStringValue(formMap, linkids[i])));
		queryList.add(new QueryParamDomain("status", "0"));
		List list = bussinessFacade.query(formDomain.getMaptable(), queryList);
		setResponseMsg("formmap", formMap);
		setResponseMsg("datalist", list);
		
		return "openerlist";
	}
	
	public String openeradd() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if((!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.UPDATE)) && ((!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.SAVE))))
			return "noright";
		setResponseMsg("formmap", formMap);
		
		return "openeradd";
	}
	
	public String openermod() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if((!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.UPDATE)) && ((!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.SAVE))))
			return "noright";
		try{
			bussinessFacade.createConn();
			List queryList = new ArrayList();
			HashMap dataMap = new HashMap();
			if(!StringUtil.isEmpty(formDomain.getMaptable())) {
				if(MapUtil.isEmpty(formMap, "id"))
					queryList.add(new QueryParamDomain(formDomain.getLinkid(), MapUtil.getStringValue(formMap, formDomain.getLinkid())));
				else
					queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id")));
				dataMap = bussinessFacade.getByID(formDomain.getMaptable(), queryList);
			}
			if((formDomain.getDynTableList() != null) && (formDomain.getDynTableList().size() > 0)) {
				queryList = new ArrayList();
				String curid = MapUtil.getStringValue(dataMap, "id");
				if(StringUtil.isEmpty(curid)) {
					curid = MapUtil.getStringValue(formMap, formDomain.getDynTableParentid());
					dataMap.put(formDomain.getDynTableParentid(), curid);
				}
				if(StringUtil.isEmpty(curid)) {
					curid = MapUtil.getStringValue(formMap, "id");
					dataMap.put(formDomain.getDynTableParentid(), curid);
				}
				queryList.add(new QueryParamDomain(formDomain.getDynTableParentid(), curid));
				List curList = bussinessFacade.query(formDomain.getDynTableName(), queryList);
				dataMap.put(DynamicFormHelper.DYNTABLE_FORM_KEY, curList);
			}
			if(dataMap.size() == 0)
				dataMap = formMap;
			dataMap.put("USER_DISTRICTCODE", MapUtil.getStringValue(formMap, "USER_DISTRICTCODE"));
			setResponseMsg("formmap", formMap);
			setResponseMsg("datamap", dataMap);
		}finally{
			bussinessFacade.closeConn();
		}
		
		return "openermod";
	}	
	
	public String viewlist() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if((!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.VIEW)) && (!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode() + "_REC", Globals.VIEW)))
			return "noright";
		List queryList = new ArrayList();
		String[] linkids = formDomain.getLinkid().split("~");
		for(int i=0; i<linkids.length; i++)
			queryList.add(new QueryParamDomain(linkids[i], MapUtil.getStringValue(formMap, linkids[i])));
		queryList.add(new QueryParamDomain("status", "0"));
		List list = bussinessFacade.query(formDomain.getMaptable(), queryList);
		setResponseMsg("formmap", formMap);
		setResponseMsg("datalist", list);
		
		return "viewlist";
	}
	
	public String openerview() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if((!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.VIEW)) && (!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode() + "_REC", Globals.VIEW)))
			return "noright";
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id")));
		HashMap dataMap = bussinessFacade.getByID(formDomain.getMaptable(), queryList);
		setResponseMsg("formmap", formMap);
		setResponseMsg("datamap", dataMap);
		
		return "openerview";
	}
	
	public String openerdelete() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if((!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.UPDATE)) && ((!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.SAVE))))
			return "noright";
		TableInfoDomain tableinfodomain = bussinessFacade.getColumnInfo(formDomain.getMaptable());
		if(tableinfodomain.getColumnkeymap().containsKey("status")) {
			formMap.put("status", "1");
			dynamicFormFacade.dealBussiness(bussinessFacade, formDomain, formMap);
		} else {
			List queryList = new ArrayList();
			queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id")));
			bussinessFacade.delete(formDomain.getMaptable(), queryList);
		}
		formMap.put("is_closed", "1");
		setResponseMsg("formmap", formMap);
		
		return "openermod";
	}
	
	public String openlist() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if(getUserInfo() == null)
			return "noright";
		List queryList = dynamicFormFacade.getQueryList(formDomain, formMap);
		if(MapUtil.isEmpty(formMap, "status"))
			queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = bussinessFacade.queryPaged(formDomain.getMaptable(), queryList, "", MapUtil.getIntValue(formMap, "page", 1)
				, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "openlist";
	}
	
	public String selectlist() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if(getUserInfo() == null)
			return "noright";
		if(formDomain.isQueryprefix())
			formMap.put("status", "0");
		List queryList = dynamicFormFacade.getQueryList(formDomain, formMap);
		String sql = formDomain.getQuerysql();
		if((StringUtil.isEmpty(sql)) && (!formDomain.getOperatorbut().equals("NO")))
			queryList.add(new QueryParamDomain("status", "0"));
		Pagination pagination = null;
		if(StringUtil.isEmpty(sql))
			pagination = bussinessFacade.queryPaged(formDomain.getMaptable(), queryList, formDomain.getOrderby(), MapUtil.getIntValue(formMap, "page", 1)
					, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		else {
			String groupby = "";
			int index = sql.indexOf(" group by ");
			if(index > 0){
				sql = sql.substring(0, index);
				groupby = formDomain.getQuerysql().substring(index);
			}
			pagination = bussinessFacade.queryWithPrefixPaged(sql, queryList, formDomain.getOrderby() + groupby, MapUtil.getIntValue(formMap, "page", 1)
					, MapUtil.getIntValue(formMap, "numperpage", Globals.getPageSize()));
		}
		setResponseMsg("paginlist", pagination);
		setResponseMsg("formmap", formMap);
		
		return "selectlist";
	}
	
	public String workflowlist() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if(getUserInfo() == null)
			return "noright";
		List queryList = new ArrayList();
		queryList.add(new QueryParamDomain("wfid", MapUtil.getStringValue(formMap, "wfid")));
		List datalist = bussinessFacade.queryWithPostfix("wf_record", queryList, formDomain.getOrderby());
		setResponseMsg("datalist", datalist);
		setResponseMsg("formmap", formMap);
		return "workflowlist";
	}
	
	public String openlistjson() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if(getUserInfo() == null)
			return "noright";
		List queryList = dynamicFormFacade.getQueryList(formDomain, formMap);
		queryList.add(new QueryParamDomain("status", "0"));
		List list = bussinessFacade.query(formDomain.getMaptable(), queryList);
		String message = MapUtil.listMapToJsonStr(list);
		responseMsg(message);
		
		return null;
	}
	
	public String delete() {
		HashMap formMap = getFormMapWithUser();
		String formid = MapUtil.getStringValue(formMap, "formid");
		if(StringUtil.isEmpty(formid))
			return "noright";
		FormDomain formDomain = dynamicFormFacade.getForm(formid);
		if((!formDomain.isIsdelete()) || (!UserDomainHelper.isAllow(ServletActionContext.getRequest(), formDomain.getPrecode(), Globals.UPDATE)))
			return "noright";
		try{
			bussinessFacade.beginTran();
			List queryList = new ArrayList();
			String delattribute = formDomain.getDelattribute();
			queryList.add(new QueryParamDomain("id", MapUtil.getStringValue(formMap, "id"))); 
			bussinessFacade.delete(formDomain.getMaptable(), queryList);
			if(delattribute.length() > 0) {
				String[] temps = delattribute.split(";");
				for(int i=0; i<temps.length; i++) {
					String[] temp2 = temps[i].split("\\|");
					String[] temp3 = temp2[1].split(",");
					queryList = new ArrayList();
					for(int j=0; j<temp3.length; j++) {
						String[] temp4 = temp3[j].split("~");
						queryList.add(new QueryParamDomain(temp4[1], MapUtil.getStringValue(formMap, temp4[0]))); 
					}
					bussinessFacade.delete(temp2[0], queryList);
				}
			}
			bussinessFacade.commitTran();
			if(formDomain.isIscache())
				BussinessQueryHelper.removeByKey(formDomain.getMaptable().toUpperCase());
        	if(!StringUtil.isEmpty(formDomain.getCacherefreshcode()))
				CacheRefreshUtil.refreshByCode(formDomain.getCacherefreshcode(), formMap);
		} catch(Exception ex) {
			bussinessFacade.rollbackTran();
		}
		String url = "DynamicForm_list.action?formid=" + formid;
		if(!StringUtil.isEmpty(formDomain.getBacksuffix()))
			url += "&" + formDomain.getBacksuffix();
		if(formDomain.isIscache())
			BussinessQueryHelper.removeByKey(formDomain.getMaptable().toUpperCase());
    	setResponseUrl(formMap, url);
		
		return null;
	}

	public java.util.List<File> getUpload() {
		return upload;
	}

	public void setUpload(java.util.List<File> upload) {
		this.upload = upload;
	}

	public java.util.List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(java.util.List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public java.util.List<String> getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(java.util.List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
}
